#include <string>
#include <iostream>
#include <windows.h>
#include "Resource.h"
#include "YujcListener.h"

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ConnectDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK AboutDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);
HWND hWnd;
HWND hConnectWnd;
HWND hAboutWnd;
HINSTANCE hInst;
YujcListener * JabberHelper;

void Thread(void * pParams)
{
	Client * client = reinterpret_cast<Client *>(pParams);
	while(1){
		client->recv(100);
		Sleep(100);
	}
}

void AddUserToComboBox(string jid, string username)
{
	SendMessageA(GetDlgItem(hWnd, IDC_USERSLISTBOX), LB_ADDSTRING, 0, (LPARAM)username.c_str());
}

void PrintMessage(string message)
{
	SendMessageA(GetDlgItem(hWnd, IDC_CHATLISTBOX), LB_ADDSTRING, 0, (LPARAM)message.c_str());
}

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	hInst = hInstance;
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), hWnd, reinterpret_cast<DLGPROC>(DlgProc));
	return FALSE;
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	hWnd = hWndDlg;
	switch(Msg)
	{
	case WM_INITDIALOG:
		{
			DialogBox(hInst, MAKEINTRESOURCE(IDD_CONNECTDIALOG), hConnectWnd, reinterpret_cast<DLGPROC>(ConnectDlgProc));
			return TRUE;
		}
	case WM_COMMAND:
	{
		if(HIWORD(wParam) == LBN_DBLCLK)
		{
			char buffer [33];
			HWND hwndUserListBox = GetDlgItem(hWndDlg, IDC_USERSLISTBOX);
			_ASSERTE(hwndUserListBox != NULL);

			int itemIndex = (int) SendMessage(hwndUserListBox, LB_GETCURSEL, (WPARAM)0, (LPARAM) 0);
			if (itemIndex == LB_ERR)
			{
				return TRUE;
			}
			itoa(itemIndex, buffer, 10);
			MessageBoxA(GetActiveWindow(), (LPCSTR)JabberHelper->getUserJIDByIndex(itemIndex).c_str(), (LPCSTR)buffer, MB_ICONERROR );
		}
		switch(wParam)
		{
		case IDOK:
			{ 
				if (JabberHelper)
				{
					char msg[200];
					if (GetDlgItemTextA(hWndDlg, IDC_MSG, (LPSTR)msg, 200))
					{
						string tmp_msg;
						for(size_t i=0; i<200; ++i)
							tmp_msg=tmp_msg+msg[i];
						JabberHelper->chat_room->send(tmp_msg);
						PrintMessage(tmp_msg);
						SetDlgItemTextA(hWndDlg, IDC_MSG, "");
					}
				}
				else
				{
					char * text = "Please,  connect!";
					char * zag = "ERROR ";
					MessageBoxA(GetActiveWindow(), (LPCSTR)text , (LPCSTR)zag, MB_ICONERROR );
				}
				return TRUE;
			}
		case MENU_SETTINGS:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_CONNECTDIALOG), hConnectWnd, reinterpret_cast<DLGPROC>(ConnectDlgProc));
			return TRUE;
		case MENU_EXIT:
			EndDialog(hWndDlg, 0);
			return TRUE;
		case MENU_ABOUT:

			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hAboutWnd, reinterpret_cast<DLGPROC>(AboutDlgProc));
			return TRUE;
		}
		break;
	}
	case WM_CLOSE:
		EndDialog(hWndDlg, 0);
		return TRUE;
		break;

	}
	return FALSE;
}

LRESULT CALLBACK ConnectDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case WM_INITDIALOG:
			SetDlgItemTextA(hWndDlg, IDC_ACCEDIT, "yu");
			SetDlgItemTextA(hWndDlg, IDC_PASS, "1234");
			return TRUE;
	case WM_COMMAND:
		switch(wParam)
		{
		case IDOK:
			{
				char accName[255];
				int length = GetDlgItemTextA(hWndDlg, IDC_ACCEDIT, (LPSTR)accName, 255);
				char pass[40];
				int legth = GetDlgItemTextA(hWndDlg, IDC_PASS, (LPSTR)pass, 20);
				JabberHelper = new YujcListener(accName, pass, &PrintMessage, &AddUserToComboBox);
				JabberHelper->connect(&Thread);
				EndDialog(hWndDlg, 0);
				break;
			}
		case ID_CANCEL:
			{
				EndDialog(hWndDlg, 0);
				return TRUE;
			}
		}
	}
	return FALSE;
}

LRESULT CALLBACK AboutDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case WM_INITDIALOG:	return TRUE;
	case WM_COMMAND:
		switch(wParam)
		{
			case IDOK:
			{
				EndDialog(hWndDlg, 0);
				return TRUE;
				break;
			}
		}
	}
	return FALSE;
}